﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo.CommonBase.Logging;
using Ext.Net;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.DTO.Infos;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.View.Forms;
using Demo.Services.View.Controllers;
using Demo.Services.Contracts.DTO.Infos;
using System.Globalization;
using Demo.Services.Services.Impl.Managers;

namespace Demo.Web.Operation
{
    public partial class SearchOperationView : PageBase
    {


        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                pnFilters.Collapse();               
            }            
        }

        protected void BtnSearch_Click(object sender, DirectEventArgs e)
        {
            StoreReadDataEventArgs args;
            try
            {
                Logger.Info();
                stOperations.RemoveAll();
                stOperations.Reload();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }            
        }

        protected void BtnClean_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();
                txtProyecto.Text = string.Empty;
                txtResource.Text = string.Empty;
                dtDateStart.Text = string.Empty;
                dtDateEnd.Text = string.Empty;
                stOperations.RemoveAll();
                stOperations.Reload();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

        protected void BtnNew_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();



                OperationPopup.Title = "Nueva Operación";
                OperationPopup.Show();
                ComponentLoader loader = new ComponentLoader();
                loader.AutoLoad = true;
                loader.Url = "OperationView.aspx";
                OperationPopup.LoadContent(loader);
                
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

        protected void BtnDeleteConfirm_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();

                int id = 0;
                if (e.ExtraParams["id"] != null)
                {
                    id = Convert.ToInt32(e.ExtraParams["id"]);
                }

                X.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
                {
                    Yes = new MessageBoxButtonConfig
                    {
                        Handler = string.Format("App.direct.DoYes({0})", id),
                        Text = "Si"
                    },
                    No = new MessageBoxButtonConfig
                    {
                        Handler = "App.direct.DoNothing()",
                        Text = "No",
                    }
                }).Show();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

        protected void BtnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();
                int id = 0;
                if (e.ExtraParams["id"] != null)
                {
                    id = Convert.ToInt32(e.ExtraParams["id"]);
                }

                X.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
                {
                    Yes = new MessageBoxButtonConfig
                    {
                        Handler = string.Format("App.direct.DoYes({0})", id),
                        Text = "Si"
                    },
                    No = new MessageBoxButtonConfig
                    {
                        Handler = "App.direct.DoNothing()",
                        Text = "No",
                    }
                }).Show();                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }

        }

        [DirectMethod]
        public void DoYes(int id)
        {
            OperationManager operation;
            try
            {
                operation = new OperationManager();
                operation.DeleteOperationLineTime(id);
                Notification.Show(new NotificationConfig
                {
                    Title = "Información",
                    Icon = Icon.Information,
                    Html = "El registro fué borrado",
                    HideDelay = 3000
                });
                stOperations.RemoveAll();
                stOperations.Reload();
            }
            catch (Exception)
            {                
                throw;
            }
           
        }

        [DirectMethod]
        public void DoNothing()
        {
            return;
        }
        
        protected void grdOperationOperation_ReadData(object sender, StoreReadDataEventArgs e)
        {
            try
            {
                OperationFilter filter;
                OperationController controller;
                OperationPagingInfo operationPaging;
                try
                {
                    Logger.Info();

                    filter = GetFilter();
                    filter.Pagination = new PaginationInfo();
                    filter.Pagination.PageNum = e.Start ;//== e.Limit ? 0 : e.Start;
                    filter.Pagination.PageSize = e.Limit;

                      

                    SetValue("OperationFilter", filter);
                    controller = new OperationController();
                    controller.ExecuteService(this, "Services.GetOperations");
                    operationPaging = (OperationPagingInfo)GetValue("OperationPaging");

                    if (operationPaging != null)
                    {
                        
                        e.Total = operationPaging.Pagination.TotalCount;
                        stOperations.DataSource = operationPaging.Operations;
                        stOperations.DataBind();                       
                    }                    
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                    throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
                }
            
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
               
        #endregion


        #region Methods

        private void FillGridOperations()
        {
            OperationFilter filter;
            OperationController controller;
            OperationPagingInfo operationPaging;
            try
            {
                Logger.Info();

                filter = GetFilter();

                SetValue("OperationFilter", filter);
                controller = new OperationController();
                controller.ExecuteService(this, "Services.GetOperations");
                operationPaging = (OperationPagingInfo)GetValue("OperationPaging");

                stOperations.DataSource = operationPaging.Operations;
                stOperations.DataBind();                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        
        private OperationFilter GetFilter()
        {
            OperationFilter filter;
            try
            {
                Logger.Info();
                filter = new OperationFilter();
                filter.Proyect = txtProyecto.Text;
                filter.Resource = txtResource.Text;
                CultureInfo culture = new CultureInfo("es-MX");                

                filter.DateStart = dtDateStart.SelectedValue == null ? string.Empty : DateTime.Parse(dtDateStart.SelectedValue.ToString(),culture).ToString("MM/dd/yyyy");
                filter.DateEnd = dtDateEnd.SelectedValue == null ? string.Empty : DateTime.Parse(dtDateEnd.SelectedValue.ToString(), culture).ToString("MM/dd/yyyy"); 
                filter.Pagination = new PaginationInfo(0,50,"Proyecto","ASC" );               
                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return filter;

        }

        #endregion       
        
    }
}